/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: ListRolesPresenter.java
 * Date: 04/02/2011
 */

package com.ingenius.sau.client.presenter;

import java.util.List;

import com.extjs.gxt.ui.client.data.BasePagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.ingenius.sau.client.AppController;
import com.ingenius.sau.client.Constants;
import com.ingenius.sau.client.event.EditRoleEvent;
import com.ingenius.sau.client.service.UsersServiceAsync;
import com.ingenius.sau.client.view.Display;
import com.ingenius.sau.shared.model.users.Role;
import com.ingenius.sau.shared.util.filter.Filter;

/**
 * Class <strong>ListRolesPresenter</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class ListRolesPresenter implements ListPresenter<Role> {

	public interface ListRolesDisplay extends Display {
		void reload();
	}

	private final HandlerManager eventBus;
	private final UsersServiceAsync rpcService;
	private final ListRolesDisplay display;
	private String selectedRoleId;

	/**
	 * 
	 * @param eventBus
	 * @param rpcService
	 * @param display
	 */
	public ListRolesPresenter(HandlerManager eventBus,
			UsersServiceAsync rpcService, ListRolesDisplay display) {

		this.rpcService = rpcService;
		this.eventBus = eventBus;
		this.display = display;

		display.setPresenter(this);
		selectedRoleId = null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.mvp.presenters.ListPresenter#doAdd()
	 */
	@Override
	public void doAdd() {
		fireEvent(new EditRoleEvent(null));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.mvp.presenters.ListPresenter#doEdit()
	 */
	@Override
	public void doEdit() {
		fireEvent(new EditRoleEvent(selectedRoleId));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.mvp.presenters.ListPresenter#doDelete()
	 */
	@Override
	public void doDelete() {
		// TODO i18n
		final String failedMessage = "Failed to delete role";
		final String successMessage = "Role deleted sucessfully";

		AppController.setStatusMessage("Deleting...");
		rpcService.deleteRole(selectedRoleId, new AsyncCallback<Boolean>() {
			@Override
			public void onFailure(Throwable caught) {
				GWT.log(Constants.MESSAGE_FAILED_DELETE + Role.class.getName(),
						caught);
				display.showInfo(failedMessage);
				hideStatusMessage();
			}

			@Override
			public void onSuccess(Boolean result) {
				if (result.booleanValue() == true) {
					display.showInfo(successMessage);
					hideStatusMessage();

				} else {
					onFailure(new Throwable("Database error"));
				}
			}

			void hideStatusMessage() {
				AppController.hideStatusMessage();
				display.reload();
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.mvp.presenters.ListPresenter#goInto()
	 */
	@Override
	public void goInto() {
		return;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.presenter.ListPresenter#fireEvent()
	 */
	@Override
	public void fireEvent(GwtEvent<?> event) {
		eventBus.fireEvent(event);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.presenters.ListPresenter#setSelectedItemId
	 * (java.lang.String)
	 */
	@Override
	public void setSelectedItemId(String id) {
		this.selectedRoleId = id;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.presenters.ListPresenter#loadData(java.lang
	 * .List, java.lang.Object, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void loadData(List<Filter> filters, Object config,
			AsyncCallback<PagingLoadResult<Role>> callback) {

		rpcService.findRoles(filters, (BasePagingLoadConfig) config, callback);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.presenters.Presenter#go(com.google.gwt.user
	 * .client.ui.HasWidgets)
	 */
	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
	}

	@Override
	public String getModuleName() {
		return Constants.TOKEN_USERS;
	}
}
